package com.ylq.impl;

import com.ylq.dao.RoleModulesDao;
import com.ylq.entity.Modules;
import com.ylq.entity.TreeDto;
import com.ylq.service.RoleModulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author qiang
 * @Description TODO
 * @Date 2020-05-02
 **/
@Service
public class RoleModulesServiceImpl implements RoleModulesService{
    @Autowired
    private RoleModulesDao roleModulesDao ;

    @Override
    public List<Modules> selByRoleId(String roleId) {
        return roleModulesDao.selByRoleId(roleId);
    }
    //将查询的模块信息封装成树节点
    public List<TreeDto> encapsulationTree(String roleId){
        List<Modules> list = selByRoleId(roleId);
        //声明树节点的集合
        List<TreeDto> treeDtos = new ArrayList<>();
        for(Modules modules:list){
            TreeDto dto = new TreeDto();
            dto.setId(modules.getId());
            dto.setTitle(modules.getTitle());
            dto.setChecked(modules.isChecked());
            dto.setPid(modules.getParentId());
            dto.setHref(modules.getPath());
            treeDtos.add(dto);
        }
        return treeDtos;
    }

    @Override
    public List<TreeDto> turnTree(String roleId) {
        List<TreeDto> treeDtoList = encapsulationTree(roleId);
        //声明一个集合存根节点
        List<TreeDto> treeDtos = new ArrayList<TreeDto>();
        for (TreeDto trDto:treeDtoList){
            if (0==trDto.getPid()){
                treeDtos.add(trDto);
            }
            for (TreeDto it : treeDtoList){
                if (it.getPid()==trDto.getId()){
                    if(trDto.getChildren() == null){
                        trDto.setChildren(new ArrayList<TreeDto>());
                    }
                    trDto.getChildren().add(it);
                }
            }

        }
    return treeDtos;
    }

    @Override
    public int deletePid(String rId) {
        return roleModulesDao.deletePid(rId);
    }

    @Override
    public int setSysRights(String parentIds, String rId) {
        int for1 = 0;
        for (String parentIdss: parentIds.split(",")){
            for1 = roleModulesDao.setSysRights(parentIdss,rId);
        }
        return for1;
    }


}